--Addon author: Allez
local module = {}
module.name = "Button Styler"
module.Init = function()
	if not SCDB.modules[module.name] then return end
	if SCDB[module.name] == nil then SCDB[module.name] = {} end
	if SCDB[module.name]["Hide hotkeys"] == nil then SCDB[module.name]["Hide hotkeys"] = false end
	if SCDB[module.name]["Hide macro"] == nil then SCDB[module.name]["Hide macro"] = false end
	if SCDB[module.name]["Use range checker"] == nil then SCDB[module.name]["Use range checker"] = true end
	if SCDB[module.name]["Gradient"] == nil then SCDB[module.name]["Gradient"] = true end
	local opts = SCDB[module.name]
	

	local setStyle = function(bname)
		if bname:lower():find("MultiCast") then return end
		local button = _G[bname]
		local icon   = _G[bname.."Icon"]
		local flash  = _G[bname.."Flash"]
		local hotkey = _G[bname.."HotKey"]
		local macro  = _G[bname.."Name"]
		local border = _G[bname.."Border"]

		if icon then
			icon:SetTexCoord(0.1, 0.9, 0.1, 0.9)
			icon:SetPoint("TOPLEFT", button, "TOPLEFT", 2, -2)
			icon:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -2, 2)
		end
		
		if hotkey then
			if opts["Hide hotkeys"]==false then 
				hotkey:SetPoint("TOPRIGHT", -2, -2) 
				hotkey:SetFont(hotkey:GetFont(), 10, "OUTLINE")
			else
				hotkey:Hide()
			end
		end
		
		if macro then
			if opts["Hide macro"]==false then 
				local font = hotkey:GetFont()
				macro:SetFont(font, 8, "OUTLINE")
			else
				macro:Hide()
			end
		end

		if border then 
			border:Hide()
		end
		
		button:SetHighlightTexture("Interface\\Buttons\\UI-TotemBar")
		button.SetHighlightTexture = function() end
		button:GetHighlightTexture():SetTexCoord(1 / 128, 35 / 128, 207 / 256, 240 / 256)
		button:GetHighlightTexture():ClearAllPoints()
		button:GetHighlightTexture():SetPoint("TOPLEFT", icon, -2, 2)
		button:GetHighlightTexture():SetPoint("BOTTOMRIGHT", icon, 2, -2)
		button:GetNormalTexture():SetDesaturated(1)
		button:GetHighlightTexture():SetVertexColor(.5,1,0)
		button:GetHighlightTexture().SetVertexColor = function() end
		button:GetHighlightTexture().SetAlpha = function() end
		
		button:SetNormalTexture("Interface\\Buttons\\UI-TotemBar")
		button.SetNormalTexture = function() end
		button:GetNormalTexture():SetTexCoord(1 / 128, 35 / 128, 207 / 256, 240 / 256)
		button:GetNormalTexture():ClearAllPoints()
		button:GetNormalTexture():SetPoint("TOPLEFT", icon, -2, 2)
		button:GetNormalTexture():SetPoint("BOTTOMRIGHT", icon, 2, -2)
		button:GetNormalTexture():SetDesaturated(1)
		button:GetNormalTexture():SetVertexColor(1,1,1)
		button:GetNormalTexture().SetVertexColor = function() end
		button:GetNormalTexture().SetAlpha = function() end
		
		button:SetPushedTexture("Interface\\Buttons\\UI-TotemBar")
		button.SetPushedTexture = function() end
		button:GetPushedTexture():SetTexCoord(1 / 128, 35 / 128, 207 / 256, 240 / 256)
		button:GetPushedTexture():ClearAllPoints()
		button:GetPushedTexture():SetPoint("TOPLEFT", icon, -2, 2)
		button:GetPushedTexture():SetPoint("BOTTOMRIGHT", icon, 2, -2)
		button:GetNormalTexture():SetDesaturated(1)
		button:GetPushedTexture():SetVertexColor(1,0,0)
		button:GetPushedTexture().SetVertexColor = function() end
		button:GetPushedTexture().SetAlpha = function() end
		
		button:SetCheckedTexture("Interface\\Buttons\\UI-TotemBar")
		button.SetCheckedTexture = function() end
		button:GetCheckedTexture():SetTexCoord(1 / 128, 35 / 128, 207 / 256, 240 / 256)
		button:GetCheckedTexture():ClearAllPoints()
		button:GetCheckedTexture():SetPoint("TOPLEFT", icon, -2, 2)
		button:GetCheckedTexture():SetPoint("BOTTOMRIGHT", icon, 2, -2)
		button:GetCheckedTexture():SetVertexColor(0,0,1,1)
		button:GetCheckedTexture().SetVertexColor = function() end
		button:GetCheckedTexture().SetAlpha = function() end
		
		if SCDB[module.name]["Gradient"] == true then
			button:GetHighlightTexture():SetGradientAlpha("VERTICAL", 0, 0, 0, 1, 1, 1, 1, 1)
			button:GetNormalTexture():SetGradientAlpha("VERTICAL", 0, 0, 0, 1, 1, 1, 1, 1)
			button:GetPushedTexture():SetGradientAlpha("VERTICAL", 0, 0, 0, 1, 1, 1, 1, 1)
			button:GetCheckedTexture():SetGradientAlpha("VERTICAL", 0, 0, 0, 1, 1, 1, 1, 1)
		end
		
		if flash then flash:SetTexture(""); flash.SetTexture = function() end end
	end

	local modActionButton_Update = function(self)
		if not self.styled then
			local action = self.action
			local name = self:GetName()
			local button  = self
			local count  = _G[name.."Count"]
			local duration  = _G[name.."Duration"]
			local border  = _G[name.."Border"]
			
			if border then 
				border:Hide()
				border.Show = function() end
			end
			
			if count then 
				count:SetFont(count:GetFont(), 10, "OUTLINE") 
			end
			
			if duration then
				duration:SetFont(duration:GetFont(), 10)
				duration:SetShadowOffset(.5,-.5)
			end
			
			setStyle(name)
			
			self.styled = true
		end
		
		if self.action then
			local texture = GetActionTexture(self.action)
			if texture == nil then
				self.mod_range = nil
			else
				self.mod_range = -1
			end
		end
	end
	  
	local modPetActionBar_Update = function()
		for i=1, NUM_PET_ACTION_SLOTS do
			local name = "PetActionButton"..i
			local button  = _G[name]
			setStyle(name)
		end  
	end
	  
	local modShapeshiftBar_UpdateState = function()    
		for i=1, NUM_SHAPESHIFT_SLOTS do
			local name = "ShapeshiftButton"..i
			local button  = _G[name]
			setStyle(name)
		end    
	end

	local modActionButton_UpdateUsable = function(self)
		local name = self:GetName()
		local icon = _G[name.."Icon"]
		local count = _G[self:GetName().."HotKey"]
		
		local isUsable, notEnoughMana = IsUsableAction(self.action)
		if (ActionHasRange(self.action) and IsActionInRange(self.action) == 0) then
			icon:SetVertexColor(1, 0, 0)
			return
		elseif (notEnoughMana) then
			icon:SetVertexColor(.1, .3, 1)
			return
		elseif (isUsable) then
			icon:SetVertexColor(1, 1, 1, 1)
			return
		else
			icon:SetVertexColor(0.4, 0.4, 0.4, 1)
			return
		end
	end

	local modActionButton_OnUpdate = function(self, elapsed)
		if self.mod_range == nil then return end
		self.mod_range = self.mod_range + elapsed
		if (self.mod_range > TOOLTIP_UPDATE_TIME) then
			modActionButton_UpdateUsable(self)
			self.mod_range = 0
		end
	end

	local modActionButton_UpdateHotkeys = function(self, actionButtonType)
		if (not actionButtonType) then
			actionButtonType = "ACTIONBUTTON"
		end
		local hotkey = _G[self:GetName().."HotKey"]
		local macro  = _G[self:GetName().."Name"]
			
		local key = GetBindingKey(actionButtonType..self:GetID()) or GetBindingKey("CLICK "..self:GetName()..":LeftButton")
		local text = GetBindingText(key, "KEY_", 1)
		hotkey:SetText(text)
		
			
		if hotkey then
			if opts["Hide hotkeys"]==false then 
				hotkey:Show()
			else
				hotkey:Hide()
			end
		end
		
		if macro then
			if opts["Hide macro"]==false then 
				macro:Show()
			else
				macro:Hide()
			end
		end
	end

	
	hooksecurefunc("ActionButton_Update", modActionButton_Update)
	--hooksecurefunc("MultiCastSummonSpellButton_Update", modActionButton_Update)
	--hooksecurefunc("MultiCastRecallSpellButton_Update", modActionButton_Update)
	
	if SCDB[module.name]["Use range checker"] then
		hooksecurefunc("ActionButton_UpdateUsable", modActionButton_UpdateUsable)
		hooksecurefunc("ActionButton_OnUpdate",  modActionButton_OnUpdate)
	end
	
	hooksecurefunc("ShapeshiftBar_OnLoad",   modShapeshiftBar_UpdateState)
	hooksecurefunc("ShapeshiftBar_UpdateState",   modShapeshiftBar_UpdateState)
	hooksecurefunc("PetActionBar_Update",   modPetActionBar_Update)
	hooksecurefunc("ActionButton_UpdateHotkeys", modActionButton_UpdateHotkeys)

end
tinsert(SuperClassic.modules, module) -- finish him!